
from base.base import Base
from dao.mysqlhelper import MySQLHelper
import json
import concurrent.futures

def send_event_assessment(event_id):
    base = Base()
    method = 'POST'
    url = 'http://192.168.101.202/gateway/tw-tunnel-server/informationRelease/assessment.json'
    headers = {
        "Tokenauthorization": "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1aWQiOiI0Yjk1NmVlMjM1ZmI0NzJkOTllMmQ4M2FiZTc0YmIzZiIsImF1ZCI6ImlvYyIsImlzcyI6Im9hdXRoIiwicm5kIjoiejAzMHUwOHMiLCJleHAiOjM4NTk2MDg4MjEsImlhdCI6MTcxMjEyNTE3NH0.6waBppaLAnYWnb0PBzxcHqxBmfK4t1mBANgOGATJ3o4",
        "Tenantid": "T1000",
        'Content-Type': 'application/json'
    }
    data = {
        "isCasualty": "0",
        "casualtyDepict": "",
        "influence": "0",
        "occurReason": "111",
        "handleDepict": "",
        "replenishDepict": "",
        "eventFinallyLevelId": "a7b92d746ad24c9b85b7a5e24becb122",
        "evidenceFile": "",
        "handleUser": "d240d59563bd463a9dbfe86ac0d4d8da",
        "handleTime": "2024-06-20 18:23:19",
        "handlecondition": "0",
        "eventId": event_id
    }
    response = base.send_request(method, url, param_type=1, data=data, headers=headers)
    return json.loads(response)

def main():
    mysql = MySQLHelper()
    sql = 'select event_id from event_info where status=1'
    result = mysql.select(sql)

    # 使用线程池进行并发请求
    with concurrent.futures.ThreadPoolExecutor(max_workers=50) as executor:
        future_to_eventId = {executor.submit(send_event_assessment, event_id[0]): event_id for event_id in result}
        for future in concurrent.futures.as_completed(future_to_eventId):
            event_id = future_to_eventId[future]
            try:
                res = future.result()
                print(f"Response for event ID {event_id[0]}: {res}")
            except Exception as exc:
                print(f"Request for event ID {event_id[0]} generated an exception: {exc}")


# 并发处理待处理的所有事件,需要更新一下token
if __name__ == '__main__':
    main()